home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11013 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  58 lines

  1. Path: news.unt.edu!news
  2. From: Steve Fogoros <sfogoros@hsc.unt.edu>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Rand() Function
  5. Date: Thu, 21 Mar 1996 09:01:27 -0800
  6. Organization: University of North Texas Health Science Center
  7. Message-ID: <31518B67.3914@hsc.unt.edu>
  8. References: <4iqltj$40g@news1.sympatico.ca>
  9. NNTP-Posting-Host: sfogoros.hsc.unt.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Gisele Swinson wrote:
  16. > How do I get a random number from a "SET" of numbers.
  17. > e.g.
  18. >       I want a random number from this set:
  19. >       2, 4, 6, 8, 10
  20. > It seems so simple but I can't find anything in my text how to do this.
  21. > Any help would be appreciated.
  22. > Thanks
  23. > Gisele
  24. > gisele.swinson@sympatico.ca
  25.  
  26. Try:
  27.  
  28.  
  29.    #include <stdio.h>
  30.  
  31.    int set[] = { 2,4,6,8,10 };
  32.  
  33.    main()
  34.    {
  35.       int i, j;
  36.  
  37.       for(j=0;j<100;j++)
  38.       {
  39.          i = rand() % (sizeof(set) / sizeof(int));
  40.          printf("%d ",set[i]);
  41.       }
  42.    }
  43.  
  44.  
  45. -- 
  46. Steve Fogoros, Academic Information Coordinator
  47. University of North Texas Health Science Center
  48. sfogoros@hsc.unt.edu
  49.